package com.simple.util.base.extend;

import com.simple.util.time.DateUtil;
import com.simple.util.time.LunarCalendarUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: simple_tools
 * @description: 学校学期年份处理工具类
 * @author: Mr.chen
 * @create: 2020-06-09 10:35
 **/
public class SchoolYearUtil {

    /**
     * 默认
     */
    private final static String UNKNOWN_YEAR = "未知年份";
    private final static String UNKNOWN_SEASON = "未知季度";

    private static final String SEASON_NAMES = "学年第一学期|学年第二学期|春季|秋季|年春季|年秋季";
    /**
     * 春季
     */
    private static final String SPRING_SEASON = "春季";
    public static final String SECOND_SCHOOL_YEAR = "学年第二学期";
    public static final String SPRING_TYPE = "1";

    /**
     * 秋季
     */
    private static final String FALL_SEASON = "秋季";
    public static final String FIRST_SCHOOL_YEAR = "学年第一学期";
    private static final String FALL_TYPE = "2";

    // 春季第一天（农历日期）
    private static final String SPRING_FIRSTDAY = "-01-08";
    // 春季最后一天
    private static final String SPRING_LASTDAY = "-08-31";
    // 秋季第一天
    private static final String FALL_FIRSTDAY = "-09-01";
    // 秋季最后一天（农日日期）
    private static final String FALL_LASTDAY = "-01-07";

    /**
     * 获取春季开始日期
     *
     * @param date
     * @return
     */
    public static Date getSpringStartDate(Date date) throws ParseException {
        String year = getSchoolYear(date);
        Date beginDate = DateUtil.parseDate((Integer.parseInt(year) + 1) + SPRING_FIRSTDAY, DateUtil.YYYY_MM_DD);
        // 阳历
        return DateUtil.parseDate(LunarCalendarUtil.lunarToSolar(beginDate), DateUtil.YYYY_MM_DD);
    }

    /**
     * 获取年级的上下期，没有默认为上期
     *
     * @author lijiancheng
     * @date 2018年3月23日 下午3:55:15
     * @param year_
     * @return
     */
    public static String getSchoolYearSeason(String year_) {
        if (!year_.contains(FALL_SEASON) && !year_.contains(SPRING_SEASON) && !year_.contains(FIRST_SCHOOL_YEAR) && !year_.contains(SECOND_SCHOOL_YEAR)) { // 上学期
            return year_ + FIRST_SCHOOL_YEAR;
        }
        return year_;
    }

    /**
     * 获取春季结束日期
     *
     * @param date
     * @return
     */
    public static Date getSpringEndDate(Date date) throws ParseException {
        String year = getSchoolYear(date);
        // 阳历
        return DateUtil.parseDate((Integer.parseInt(year) + 1) + SPRING_LASTDAY, DateUtil.YYYY_MM_DD);
    }

    /**
     * 获取当前日期的季度开始时间
     *
     * @param date
     * @return
     */
    public static String getSeasonStartDate(Date date) throws ParseException {
        String year = getSchoolYear(date);
        if (SPRING_TYPE.equals(getSeasonType(date))) {
            return DateUtil.format(getSpringStartDate(date), DateUtil.YYYY_MM_DD);
        } else {
            return String.valueOf(year).concat(FALL_FIRSTDAY);
        }
    }

    /**
     * 获取当前日期的季度结束时间
     *
     * @param date
     * @return
     */
    public static String getSeasonEndDate(Date date) throws ParseException {
        String year = getSchoolYear(date);
        if (SPRING_TYPE.equals(getSeasonType(date))) {
            return String.valueOf(Integer.parseInt(year) + 1).concat(SPRING_LASTDAY);
        } else {
            return DateUtil.format(getSpringStartDate(date), DateUtil.YYYY_MM_DD);
        }
    }

    /**
     * 获取学期开始日期
     *
     * @param seasonName(如：2017-2018学年第一学期, 2017春季)
     * @return
     */
    public static Date getSeasonStartDate(String seasonName) throws ParseException {
        String year = seasonName.replaceAll(SEASON_NAMES, "");
        if (year.indexOf("-") >= 0) {
            year = year.split("-")[0];
        }
        String date = null;
        if (seasonName.contains(FALL_SEASON) || seasonName.contains(FIRST_SCHOOL_YEAR)) { // 学年第一学期
            date = year + FALL_FIRSTDAY;
            return DateUtil.parseDate(date, DateUtil.YYYY_MM_DD);
        } else {
            date = (Integer.valueOf(year) + 1) + SPRING_FIRSTDAY;
            return DateUtil.parseDate(LunarCalendarUtil.lunarToSolar(DateUtil.parseDate(date, DateUtil.YYYY_MM_DD)), DateUtil.YYYY_MM_DD);
        }
    }

    /**
     * 获取学期结束日期
     *
     * @param seasonName(如：2017-2018学年第一学期, 2017春季)
     * @return
     */
    public static Date getSeasonEndDate(String seasonName) throws ParseException {
        String year = seasonName.replaceAll(SEASON_NAMES, "");
        if (year.indexOf("-") >= 0) {
            year = year.split("-")[0];
        }
        String date = null;
        if (seasonName.contains(FALL_SEASON) || seasonName.contains(FIRST_SCHOOL_YEAR)) { // 学年第一学期
            date = (Integer.valueOf(year) + 1) + FALL_LASTDAY;
            return DateUtil.parseDate(LunarCalendarUtil.lunarToSolar(DateUtil.parseDate(date, DateUtil.YYYY_MM_DD)), DateUtil.YYYY_MM_DD);
        } else {
            date = (Integer.valueOf(year) + 1) + SPRING_LASTDAY;
            return DateUtil.parseDate(date, DateUtil.YYYY_MM_DD);
        }
    }

    /***
     * 获取当前日期季度名称
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static String getSeasonName(Date date) throws ParseException {
        if (date == null) {
            return UNKNOWN_SEASON;
        }
        if (SPRING_TYPE.equals(getSeasonType(date))) {
            return SPRING_SEASON;
        } else {
            return FALL_SEASON;
        }
    }

    /**
     * 获取当前日期季度类型
     *
     * @param date
     * @return
     */
    public static String getSeasonType(Date date) throws ParseException {
        Date springStartDate = getSpringStartDate(date);
        Date springEndDate = getSpringEndDate(date);
        if ((date.after(springStartDate) && date.before(springEndDate)) || (DateUtil.format(date, DateUtil.YYYY_MM_DD).equals(DateUtil.format(springStartDate, DateUtil.YYYY_MM_DD))) || (DateUtil.format(date, DateUtil.YYYY_MM_DD).equals(DateUtil.format(springEndDate, DateUtil.YYYY_MM_DD)))) {
            return SPRING_TYPE;
        } else {
            return FALL_TYPE;
        }
    }

    /***
     * 获取当前日期学年年份 ，如：2017-06-19的学年为2016
     *
     * @param date
     * @return
     */
    public static String getSchoolYear(Date date) {
        if (date == null) {
            return UNKNOWN_YEAR;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        if (month < 9) {
            cal.add(Calendar.YEAR, -1);
        }
        return String.valueOf(cal.get(Calendar.YEAR));
    }

    /**
     * 获取当前日期所属学期
     *
     * @param date
     * @return
     */
    public static String getSchoolYearName(Date date) throws ParseException {
        if (date == null) {
            return null;
        }
        String year = getSchoolYear(date);
        int iYear = Integer.parseInt(year);
        String yearName = iYear + "-" + (iYear + 1);
        return StringUtils.equals(SPRING_TYPE, getSeasonType(date)) ? yearName + SECOND_SCHOOL_YEAR : yearName + FIRST_SCHOOL_YEAR;
    }

    /**
     * 获取当前日期所属学期
     *
     * @param date
     * @return
     */
    public static String getTermYearName(Date date) throws ParseException {
        if (date == null) {
            return null;
        }
        String year = getSchoolYear(date);
        int iYear = Integer.parseInt(year);
        String yearName = iYear + "-" + (iYear + 1);
        return StringUtils.equals(SPRING_TYPE, getSeasonType(date)) ? yearName + "学年" : yearName + "学年";
    }

    /**
     * 获取当前日期所属学期
     *
     * @param date
     * @return
     */
    public static String getTermName(Date date) throws ParseException {
        if (date == null) {
            return null;
        }
        return StringUtils.equals(SPRING_TYPE, getSeasonType(date)) ? "下学期" : "上学期";
    }

    /***
     * 获取当前日期学年年份 ，如：2017-06-19的学年为2016
     *
     * @param date
     * @return
     */
    public static String getSchoolYear(String date) throws ParseException {
        if (date == null) {
            return UNKNOWN_YEAR;
        }
        Date dt = DateUtil.parseDate(date, DateUtil.YYYY_MM_DD);
        String year = getSchoolYear(dt);
        int iYear = Integer.parseInt(year);
        String yearName = iYear + "-" + (iYear + 1);
        return yearName;
    }

    /***
     * 获取当前日期学年年份 ，如：2017-06-19的学年为2016
     *
     * @param date
     * @return
     */
    public static String getSchoolYearName(String date) throws ParseException {
        if (date == null) {
            return null;
        }
        return getSchoolYearName(DateUtil.parseDate(date, DateUtil.YYYY_MM_DD));
    }

    /**
     * 获取学年开始日期
     *
     * @param year
     * @return
     */
    public static Date getStartDate(String year) throws ParseException {
        Date date = DateUtil.parseDate(year.concat(FALL_FIRSTDAY), DateUtil.YYYY_MM_DD);
        return date;
    }

    /**
     * 获取平台开始时间
     * @return
     */
    public static Date getPlatformStartDate(){
        return DateUtil.parseDate("20130101");
    }

    /**
     * 获取学年结束日期
     *
     * @param year
     * @return
     */
    public static Date getEndDate(String year) throws ParseException {
        return DateUtil.parseDate(String.valueOf(Integer.parseInt(year) + 1).concat(SPRING_LASTDAY), DateUtil.YYYY_MM_DD);
    }

    public static void main(String[] args) throws ParseException {
        // String dateString = "2018-09-04";
        // Date date = DateUtil.parse(dateString, "yyyy-MM-dd");
        // System.out.println(getSchoolYear(date));
        // System.out.println(getSeasonName(date));
        // System.out.println(getSeasonType(date));
        // System.out.println(getSeasonStartDate(date));
        // System.out.println(getSeasonEndDate(date));
        // System.out.println(getSpringStartDate(date));
        // System.out.println(getSchoolYearName(date));
        // String seasonName = "2017春季";
        // System.out.println(DateUtil.format(getSeasonStartDate(seasonName), "yyyy-MM-dd"));
        // System.out.println(DateUtil.format(getSeasonEndDate(seasonName), "yyyy-MM-dd"));
        System.out.println(DateUtil.format(getSpringStartDate(new Date()), "yyyy-MM-dd"));
        System.out.println(DateUtil.format(getSpringEndDate(new Date()), "yyyy-MM-dd"));

        System.out.println(DateUtil.format(getStartDate("2018"), "yyyy-MM-dd"));
        System.out.println(DateUtil.format(getEndDate("2018"), "yyyy-MM-dd"));
        System.out.println(getTermYearName(new Date()));
        System.out.println(getTermName(new Date()));
    }
}
